/**
 * Controller class for the Loan collection
 */
package ControlLayer;
import ModelLayer.*;
import java.util.ArrayList;
//model layer package has to be imported
public class LoanCtr
{
    private CollectionOfLoans collection;
    private AddressCtr addrCtr;
    private DVDCtr dvdCtr;

    /**
     * Constructor for objects of class LoanCtr
     */
    public LoanCtr(AddressCtr addrCtr, DVDCtr dvdCtr)
    {
        collection = new CollectionOfLoans();
        this.addrCtr = addrCtr;
        this.dvdCtr = dvdCtr;
    }

    /**
     * This will print out information on certain loan.
     */
    public void getLoan(int id)
    {
        findLoan(id).printOut();
    }
    
    /**
     * This will create a loan when a person borrows a dvd.
     */
    public void lendDVD(int perId, int loanId, int dvdId, int copyId, String borrowDate, String period)
    {
        
        addToCollection(addrCtr.getPerson(perId).makeLoan(loanId, borrowDate, period, dvdCtr.getCopy(dvdId, copyId)));
        findLoan(loanId).makeUnavailable();
    }
    
    /**
     * This will remove the loan when a person returns a dvd.
     */
    public void returnDVD(int borrowerId, int loanId)
    {
        addrCtr.getPerson(borrowerId).removeLoan(loanId);
        Loan to_be_removed = findLoan(loanId);
        to_be_removed.makeAvailable();
        collection.getLoans().remove(to_be_removed);
        
    }
    
    /**
     * This will check if a dvd is reserved.
     */
    public boolean checkReservation(int borrowerId, int dvdId, int copyId)
    {
        if (dvdCtr.isReserved(dvdId, copyId))
        {
            if(dvdCtr.toWhom(dvdId, copyId) == addrCtr.getPerson(borrowerId))
            {
                dvdCtr.getCopy(dvdId, copyId).unreserve();
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return true;
        }
    }
    
    /**
     * This will reserve a dvd for a certain person.
     */
    public void reserveDVD(int reserverId, int dvdId, int copyId)
    {
        dvdCtr.getCopy(dvdId, copyId).reserve(addrCtr.getPerson(reserverId));
        
    }
    
    /**
     * This will print out all the loans.
     */
    public void listAllLoans()
    {
        ArrayList<Loan> loans = collection.getLoans();
        System.out.println("All loans:\n");
        for (Loan loan : loans)
        {
            loan.printOut();
        }
        
    }
    
    /**
     * This will find and return a loan.
     */
    private Loan findLoan(int id)
    {
        ArrayList<Loan> loans = collection.getLoans();
        for (Loan loan : loans)
        {
            if (loan.getId() == id)
            {
                return loan;
            }
        }
        return null;
    }
    
    /**
     * This will check if a certain loan is registered.
     */
    public boolean isThere(int id)
    {
        ArrayList<Loan> loans = collection.getLoans();
        for (Loan loan : loans)
        {        
            if (loan.getId() == id)
            {
                return true;
            }
        }
        return false;
    }
    
    /**
     * This will add a loan to the collection of Loans.
     */
    private void addToCollection(Loan loan)
    {
        collection.getLoans().add(loan);
    }
    
    /**
     * This method returns AddressCtr so it can also be used in LoanUI
     */
    public AddressCtr accessAdd()
    {
        return addrCtr;
    }
    
    /**
     * This method returns DVDCtr so it can also be used in LoanUI
     */
    public DVDCtr accessDVD()
    {
        return dvdCtr;
    }
}
